package com.ln.yygh.hosp.controller;

import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ln.yygh.common.result.Result;
import com.ln.yygh.common.utils.MD5;
import com.ln.yygh.hosp.service.HospitalSetService;
import com.ln.yygh.model.hosp.HospitalSet;
import com.ln.yygh.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author: lianXiaoChen
 * @create: 2021-04-25 21:50
 * @Api swagger注解给前端看的
 */
@Api(tags = "医院设置管理")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {
    @Resource
    private HospitalSetService hospitalSerService;

    //8 医院设置锁定和解锁
    //就是把医院的状态修改一下
    @PutMapping("lockHospitalSet/{id}/{status}")
    public Result lockHospitalSet(@PathVariable Long id,
                                  @PathVariable Integer status) {
        //根据id查询医院设置信息
        HospitalSet hospitalSet = hospitalSerService.getById(id);
        //设置状态
        hospitalSet.setStatus(status);
        //调用方法
        hospitalSerService.updateById(hospitalSet);
        return Result.ok();
    }

    //9 发送签名秘钥
    @PutMapping("sendKey/{id}")
    public Result sendKey(@PathVariable Long id) {
        HospitalSet hospitalSet = hospitalSerService.getById(id);
        String signKey = hospitalSet.getSignKey();
        String hoscode = hospitalSet.getHoscode();
        //使用手机短信的形式发给对方，发给对方医院的练习人
        //使用上面技术发送，阿里大于，乐信通等都可以

        return Result.ok();
    }


    /**
     * RequestMapping支持所有请求  生成文档之后很乱
     * 我们要使用resful风格
     * getMapping  get请求
     *
     * @RequestMapping(value = "/findall",method = RequestMethod.GET)
     * postMapping  post请求
     * requestMapping  加配置请求
     * @ApiOperation("获取所有医院设置")swagger注解，声明这个方法有什么用途
     */
    @ApiOperation("获取所有医院设置")
    @GetMapping("/findAll")
    private Result findall() {
        List<HospitalSet> list = hospitalSerService.list();
        Result<List<HospitalSet>> ok = Result.ok(list);
        return ok;

    }

    /**
     * PathVariable路径参数变量
     * admin/hosp/hospitalSet/xx
     * admin/hosp/hospitalSet/2
     * admin/hosp/hospitalSet/deleteHospById/xx
     */
    @DeleteMapping("/deleteHospSet/{id}")
    @ApiOperation(value = "逻辑删除医院设置")
    public Result deleteHospById(@PathVariable Long id) {
        boolean flag = hospitalSerService.removeById(id);
        if (flag) {
            //操作成功了，没有数据需要带回
            return Result.ok();
        } else {
            //删除失败
            return Result.fail();
        }
    }

    /**
     * 和上面的方法在调用上面有区别
     * admin/hosp/hospitalSet/deleteHospById2?id=xxx
     * 400,参数类型不行
     * 405，请求方法不对
     * 415：协议头不对
     */
    @GetMapping("/deleteHospById2")
    public Result deleteHospById2(String id) {
        boolean flag = hospitalSerService.removeById(Long.valueOf(id));
        if (flag) {
            //操作成功了，没有数据需要带回
            return Result.ok();
        } else {
            //删除失败
            return Result.fail();
        }
    }

    //添加信息
    @PostMapping("/saveHospitalSet")
    @ApiOperation("添加医院")
    private Result saveHospitalSet(@RequestBody HospitalSet hospitalSet) {
        //设置状态 1 使用 0 不能使用
        hospitalSet.setStatus(1);
        //签名秘钥
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt(1000)));
        //调用service
        boolean save = hospitalSerService.save(hospitalSet);
        if (save) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    //修改
    @PostMapping("/updateByHospById")
    @ApiOperation("修改医院")
    private Result updateByHospById(@RequestBody HospitalSet hospitalSet) {
        boolean flag = hospitalSerService.updateById(hospitalSet);
        if (flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    //条件查询+分页
    @PostMapping("/selectPageConnHospal/{current}/{limit}")
    @ApiOperation("条件查询+分页")
    private Result selectPageConnHospal(@PathVariable long current, @PathVariable long limit,
                                        @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo) {
        //创建page对象传递当前页码以及每页条数
        Page page = new Page(current, limit);
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();//条件钩子函数
        if (hospitalSetQueryVo != null) {
            //医院名称
            String hosname = hospitalSetQueryVo.getHosname();
            //医院编号
            String hoscode = hospitalSetQueryVo.getHoscode();
            if (hosname != null && hosname.length() >= 1) {
                wrapper.like("hosname", hospitalSetQueryVo.getHosname());
            }

            //spring提供的一个string的工具类
            //StringUtils.isEmpty(hoscode) 判断等于空  null或者  ""
            if (!StringUtils.isEmpty(hoscode)) {
                wrapper.eq("hoscode", hospitalSetQueryVo.getHoscode());
            }
            if (!StringUtils.isEmpty(hospitalSetQueryVo.getContactsName())) {
                wrapper.eq("contacts_name", hospitalSetQueryVo.getContactsName());
            }
        }
        //把条件丢失了
        //分页是好的，但是把查询条件丢失了，我们要是同步开发，肯定没有，但是别忘了这是异步
        //页面一般都是vue或者angular，双向绑定的，页面上有
        Page<HospitalSet> hospitalSetPage = hospitalSerService.page(page, wrapper);
        return Result.ok(hospitalSetPage);
    }

    //根据id查询
    @PostMapping("/selectHospalById/{id}")
    @ApiOperation("根据主键查询")
    private Result selectHospalById(@PathVariable Long id) {
        HospitalSet flag = hospitalSerService.getById(id);
        return Result.ok(flag);
    }

    //批删
    @DeleteMapping("deleteHospAll")
    public Result deleteHospAll(@RequestBody List<Long> idList) {
        hospitalSerService.removeByIds(idList);
        return Result.ok();
    }
}
